package com.kaki.mdb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author Kaki Nakajima
 * @description DB Show 数据控制层
 * @date 2024-09-17 10:11
 */
public class DBAccess {
    private Statement statement;
    public Connection connection;
    public ResultSet resultSet;

    String[] rsStrArr= null;

    // sqlite
    public DBAccess(String mdbPath){

        if(connection == null){
            try {
                Class.forName("org.sqlite.JDBC");
                Properties info = new Properties();
                info.put("charSet","GBK");
                info.put("user","");
                info.put("password","");
                connection = DriverManager.getConnection("jdbc:sqlite://" + mdbPath);
                statement = connection.createStatement();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    // sqlite
    public DBAccess(String mdbType,String mdbPath){

        if ("MDB".equals(mdbType)){
            if(connection == null){
                try {
                    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                    Properties info = new Properties();
                    info.put("password","xxx");
                    connection = DriverManager.getConnection("jdbc:ucanaccess://" + mdbPath+";openExclusive=false;ignoreCase;jackcessOpener=com.kaki.mdb.CryptCodecOpener",info);
                    statement = connection.createStatement();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }else if("Sqlite".equals(mdbType)){
            if(connection == null){
                try {
                    Class.forName("org.sqlite.JDBC");
                    Properties info = new Properties();
                    info.put("charSet","GBK");
                    info.put("user","");
                    info.put("password","");
                    connection = DriverManager.getConnection("jdbc:sqlite://" + mdbPath);
                    statement = connection.createStatement();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 查询
     * @param sql
     * @return
     */
    public ResultSet excuteQuery(String sql){
        try {
            statement = this.connection.createStatement();
            resultSet = statement.executeQuery(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultSet;
    }

    /**
     * 增删改
     * @param sql
     * @return
     */
    public int excuteUpdate(String sql){
        int ret = 0;
        try {
            ret = statement.executeUpdate(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 执行查询
     * @param sql
     * @return
     */
    public boolean queryInfo(String sql){
        boolean isHave = false;
        try {
            statement = this.connection.createStatement();
            resultSet = statement.executeQuery(sql);
            if (resultSet != null && resultSet.next()){
                isHave = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return isHave;
    }

    /*
     * 将sql语句加入到批处理
     */
    public void addBatch(String sql){
        try {
            statement.addBatch(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
